#include <iostream>
#define MAX_LEN 1000
using namespace std;
//upper triangular matrix 
int acc(int i, int j,int& n) {
    if (i < j)
        swap(i, j);
    return i * (i + 1)/2 + j;
}
void add(int a[], int b[], int add_a_b[], int n) {
    int len = (1 + n) * n / 2;
    for (int i = 0; i < len; i++) {
        add_a_b[i] = a[i] + b[i];
    }
}
void mult(int a[], int b[], int mult_a_b[], int n) {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j <= i; j++) {
            mult_a_b[acc(i, j, n)] = 0;
            for (int k = 0; k < n; k++)
                mult_a_b[acc(i, j, n)] += a[acc(i, k, n)] * b[acc(k, j, n)];
        }
    }
}

int main()
{
    std::cout << "输入阶数";
    int n, a[MAX_LEN], b[MAX_LEN], add_a_b[MAX_LEN], mult_a_b[MAX_LEN];
    cin >> n;
    int len = (1 + n) * n / 2;
    cout << "输入a矩阵" << endl;
    for (int i = 0; i < len; i++) {
        cin >> a[i];
    }
    cout << "输入b矩阵" << endl;
    for (int i = 0; i < len; i++) {
        cin >> b[i];
    }
    add(a, b, add_a_b, n);
    cout << "相加的结果是：" << endl;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cout << add_a_b[acc(i, j, n)] << " ";
        }
        cout << endl;
    }
    mult(a, b, mult_a_b, n);
    cout << "相乘的结果是：" << endl;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cout << mult_a_b[acc(i, j, n)]<<" ";
        }
        cout << endl;
    }
}




